package test;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.util.Scanner;

public class Scan {
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;

	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		try {
			connection = ConnectionFactory.createConnection(configuration);
			admin = connection.getAdmin();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void close() {
		try {
			if (admin != null)
				admin.close();
			if (null != connection)
				connection.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void getData(String name) throws IOException{
		init();
		Table table=connection.getTable(TableName.valueOf(name));
		org.apache.hadoop.hbase.client.Scan scan=new org.apache.hadoop.hbase.client.Scan();
		ResultScanner scanner=table.getScanner(scan);
		for(Result result:scanner)
			showCell(result);
		close();
	}
	public static void showCell(Result result) {
		Cell[] cells=result.rawCells();
		for(Cell cell:cells) {
			System.out.println("RowName(行键):"+new String(CellUtil.cloneRow(cell))+" ");
			System.out.println("Timetamp(时间戳):"+cell.getTimestamp()+" ");
			System.out.println("column Family（列簇）:"+new String(CellUtil.cloneFamily(cell))+" ");
			System.out.println("column Name（列名）:"+new String(CellUtil.cloneQualifier(cell))+" ");
			System.out.println("value:（值）"+new String(CellUtil.cloneValue(cell))+" ");
			System.out.println();
		}
	}
	public static void main(String[] args) throws IOException{
		Scan t=new Scan();
		System.out.println("studnt信息如下：");
		t.getData("student");
	}
}
